//
//  StatusCell.swift
//  weibosina
//
//  Created by hwm on 16/9/8.
//  Copyright © 2016年 hwm. All rights reserved.
//

import UIKit
import SDWebImage

class StatusCell: UITableViewCell {

    var status: Status? {
        didSet {
            nameLable.text = status?.user?.name
            timeLable.text = status?.created_at
            sourceLable.text = status?.source
            contentLable.text = status?.text
            // 头像
            if let url = status?.user?.imageURL {
                iconView.sd_setImageWithURL(url)
            }
            // 认证图标
            verifiedView.image = status?.user?.verifiedImage
            // 会员等级图标
            vipView.image = status?.user?.mbrankImage
        }
    }
    // 自定义一个类需要重写的init方法是 designated
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
//        self.backgroundColor = UIColor.clearColor()
        self.clipsToBounds = true
    
        contentView.clipsToBounds = true
        // 初始化UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
        // 1.添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(verifiedView)
        contentView.addSubview(nameLable)
        contentView.addSubview(vipView)
        contentView.addSubview(timeLable)
        contentView.addSubview(sourceLable)
        contentView.addSubview(contentLable)
        contentView.addSubview(footView)
        footView.backgroundColor = UIColor(white: 0.2, alpha: 0.5)
        
        // 2.布局子控件
        iconView.wm_AlignInner(type: wm_AlignType.TopLeft, referView: contentView, size: CGSize(width: 50, height: 50), offset: CGPoint(x: 10, y: 10))
        verifiedView.wm_AlignInner(type: wm_AlignType.BottomRight, referView: iconView, size: CGSize(width: 14, height: 14), offset: CGPoint(x:5, y:5))
        nameLable.wm_AlignHorizontal(type: wm_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 10, y: 0))
        vipView.wm_AlignHorizontal(type: wm_AlignType.TopRight, referView: nameLable, size: CGSize(width: 14, height: 14), offset: CGPoint(x: 10, y: 0))
        timeLable.wm_AlignHorizontal(type: wm_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: 10, y: 0))
        sourceLable.wm_AlignHorizontal(type: wm_AlignType.BottomRight, referView: timeLable, size: nil, offset: CGPoint(x: 10, y: 0))
        contentLable.wm_AlignVertical(type: wm_AlignType.BottomLeft, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 10))
        
        // 添加一个底部约束
        // TODO: 这个地方是又问题的
        //        contentLabel.wm_AlignInner(type: wm_AlignType.BottomRight, referView: contentView, size: nil, offset: CGPoint(x: -10, y: -10))
        
        let width = UIScreen.mainScreen().bounds.width
        footView.wm_AlignVertical(type: wm_AlignType.BottomLeft, referView: contentLable, size: CGSize(width: width, height: 44), offset: CGPoint(x: -10, y: 10))
        
        footView.wm_AlignInner(type: wm_AlignType.BottomRight, referView: contentView, size: nil, offset: CGPoint(x: 0, y: 0))
//        contentView.wm_AlignInner(type: wm_AlignType.BottomLeft, referView: footView, size: nil, offset: CGPointMake(-100, -100))
        
    }
   // MARK: 计算配图的尺寸
    private func calculateImageSize() -> CGSize {
        // 1.取出配图个数
        let count = status?.storedPicURLs?.count
        // 2.如果没有配图zero
        if count == 0 || count == nil {
            return CGSizeZero
        }
        // 3.如果只有一张配图, 返回图片的实际大小
        if count == 1 {
            // 3.1取出缓存的图片
            let key = status?.storedPicURLs!.first?.absoluteString
            let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(key!)
            // 3.2返回缓存图片的尺寸
            return image.size

        }
        
        // 4.如果有4张配图, 计算田字格的大小
        let width: CGFloat = 90
        let margin: CGFloat = 10
        if count == 4 {
            let viewWidth: CGFloat = width * 2 + margin
            return CGSizeMake(width, viewWidth)
        }
        // 5.如果是其它(多张), 计算九宫格的大小
        /*
         2/3
         5/6
         7/8/9
         */
        // 5.1计算列数
        let colNumber: CGFloat = 3
        // 5.2计算行数
        //               (8 - 1) / 列数 + 1
        let rowNumber: CGFloat = (CGFloat(count!) - 1) / colNumber + 1
        // 宽度 = 列数 * 图片的宽度 + (列数 - 1) * 间隙
        let viewWidth: CGFloat = colNumber * width + (colNumber - 1) * margin
        // 高度 = 行数 * 图片的高度 + (行数 - 1) * 间隙
        let viewHeight = rowNumber * width + (rowNumber - 1) * margin
        return CGSize(width: viewWidth, height: viewHeight)
        
    }
    
    // MARK: 懒加载
    /// 头像
    private lazy var iconView: UIImageView = UIImageView.init(image: UIImage.init(named: "avatar_default_small"))
    // 认证图标
    private lazy var verifiedView: UIImageView = UIImageView.init(image: UIImage.init(named: "avatar_enterprise_vip"))
    // 昵称
    private lazy var nameLable: UILabel = UILabel.creatLable(UIColor.darkGrayColor(), fontSize: 14)
    /// 会员图标
    private lazy var vipView: UIImageView = UIImageView.init(image: UIImage.init(named: "common_icon_membership"))
    // 时间
    private lazy var timeLable: UILabel = UILabel.creatLable(UIColor.grayColor(), fontSize: 14)
    // 来源
    private lazy var sourceLable: UILabel = UILabel.creatLable(UIColor.grayColor(), fontSize: 14)
    /// 正文
    private lazy var contentLable: UILabel = {
       let l = UILabel.creatLable(UIColor.darkGrayColor(), fontSize: 15)
        l.numberOfLines = 0
        l.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 20
        return l
    }()
    // 底部工具条
    private lazy var footView: StatusFootView = StatusFootView()
    
}

class StatusFootView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.redColor()
        self.clipsToBounds = true
        
        // 初始化UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        // 1.添加子控件
        addSubview(retweetBtn)
        addSubview(unlikeBtn)
        addSubview(commentBtn)
        
        // 2.布局子控件
        wm_HorizontalTile([retweetBtn, unlikeBtn, commentBtn], insets: UIEdgeInsetsMake(0, 0, 4, 0))
    }
    
    private lazy var retweetBtn: UIButton = UIButton.createButton("timeline_icon_retweet", title: "转发")
    
    private lazy var unlikeBtn: UIButton = UIButton.createButton("timeline_icon_unlike", title: "赞")
    
    private lazy var commentBtn: UIButton = UIButton.createButton("timeline_icon_comment", title: "评论")
}
